package solutions.leetcode.easy;

import solutions.lib.BaseSolution;

import java.util.LinkedList;
import java.util.List;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/binary-watch/">401. 二进制手表</a>
 */
public class Solution401 extends BaseSolution {

    public List<String> readBinaryWatch(int turnedOn) {
        List<String> rst = new LinkedList<>();
        boolean[] flag = new boolean[10];
        int pos = 0;
        check(rst, turnedOn, flag, pos);
        return rst;
    }

    private void check(List<String> rst, int turnedOn, boolean[] flag, int pos) {
        if (turnedOn <= 0) {
            int hour = 0;
            for (int i = 0; i < 4; i++) {
                hour += flag[i] ? Math.pow(2, 3 - i) : 0;
            }
            if (hour >= 12) {
                return;
            }
            int min = 0;
            for (int i = 4; i < 10; i++) {
                min += flag[i] ? Math.pow(2, 9 - i) : 0;
            }
            if (min >= 60) {
                return;
            }
            rst.add(hour + ":" + (min < 10 ? "0" : "") + min);
            return;
        }
        if (pos >= 10) {
            return;
        }
        check(rst, turnedOn, flag, pos + 1);
        flag[pos] = true;
        check(rst, turnedOn - 1, flag, pos + 1);
        flag[pos] = false;
    }

    public static void main(String[] args) {
        Solution401 solution = new Solution401();
        printVals(solution.readBinaryWatch(9));
    }
}